import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_problem.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 3/18/21.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class CountModel with ChangeNotifier {
  List<ProblemModel> _problemList = [];

  List _commitList = [];

  void initList(List<ProblemModel> list) {
    _commitList = [];
    _rightCount = 0;
    _errorCount = 0;
    _problemList = list;
    _noAnwesrCont = list.length;
    notifyListeners();
  }

  int _rightCount = 0;
  int _errorCount = 0;
  int _noAnwesrCont = 0;

  int get rightCount => _rightCount;

  int get errorCount => _errorCount;

  int get noAnwesrCont => _noAnwesrCont;

  List get commitList => _commitList;

  List<ProblemModel> get problemList => _problemList;

  void check() {
    _commitList = [];
    _rightCount = 0;
    _errorCount = 0;
    _noAnwesrCont = -1;
    _problemList.forEach((element) {
      if (element.myAnswer == null || element.myAnswer.length == 0) {
        _noAnwesrCont++;
      }
    });
    _problemList.forEach((element) {
      String rightAnswer = "Y";
      String myAnswer = "";
      List<String> myAnswerList = [];
      List<String> rightAnswerList = [];
      if (element.themAnswerStr != null) {
        element.themAnswerStr = element.themAnswerStr.toUpperCase();
        for (var i = 0; i < element.themAnswerStr.length; ++i) {
          myAnswerList.add(element.themAnswerStr.substring(i, i + 1));
        }
      }

      if (element.answer != null) {
        element.answer = element.answer.toUpperCase();
        for (var i = 0; i < element.answer.length; ++i) {
          rightAnswerList.add(element.answer.substring(i, i + 1));
        }
      } else {}
      myAnswerList.sort();
      rightAnswerList.sort();

      String checkMyAnswer = myAnswerList.toString();
      String checkRightAnswer = rightAnswerList.toString();
      if (checkMyAnswer == checkRightAnswer) {
        rightAnswer = "Y";
        _rightCount++;
      } else {
        rightAnswer = "N";
        //只有回答过才可计算错题
        if (element.themAnswerStr != null) {
          _errorCount++;
        }
      }
      myAnswerList.forEach((element) {
        myAnswer += element;
      });
      LogUtils.e(
          "answer ${element.answer} element.themAnswerStr  is ${element.themAnswerStr} ");
      element.rightAnswer = rightAnswer;
      element.myAnswer = myAnswer;
      _commitList.add(element.toJson());
    });
    notifyListeners();
  }

  void init() {
    _commitList = [];
    _problemList = [];
  }
}
